#!/usr/bin/perl

if ( $#ARGV != 5 ) {
   print STDERR "This is for internal use by attach ONLY\n" ;
   exit ;
}

$root = $ARGV[0] ;
$view = $ARGV[1] ;
$branch = $ARGV[2] ;
$label = $ARGV[3] ;
$vobname = $ARGV[4] ;
$proj = $ARGV[5] ;
$tmpname = "/tmp/config.$$" ;

$emitted = 0 ;

$ctdebug = $ENV{"CTATTACH_DEBUG"} ;

if ($ctdebug) {
   print STDERR "Params:\n  0: '$root'\n  1: '$view'\n  2: '$branch'\n" ;
   print STDERR "  3: '$label'\n  4: '$vobname'\n  5: '$proj'\n" ;
   print STDERR "making branch and label types for view " . $view . "\n" ;
   print STDERR "executing: /usr/atria/bin/cleartool mkbrtype -vob /vobs/$vobname -c \"Branch type for the $view view\" $branch 2> /dev/null > /dev/null\n" ;
   print STDERR "executing: /usr/atria/bin/cleartool mklbtype -vob /vobs/$vobname -c \"Label type for the $view view\" $label 2> /dev/null > /dev/null\n" ;
}
system "/usr/atria/bin/cleartool mkbrtype -vob /vobs/$vobname -c \"Branch type for the $view view\" $branch 2> /dev/null > /dev/null\n" ;
system "/usr/atria/bin/cleartool mklbtype -vob /vobs/$vobname -c \"Label type for the $view view\" $label 2> /dev/null > /dev/null\n" ;

if ($ctdebug) {
   print STDERR "creating/updating the config-spec for view " . $view . "\n" ;
}
open( CTINTERFACE, "/usr/atria/bin/cleartool catcs -tag $view |" ) ;
open( TMPFILE, "> $tmpname" ) ;
while ( <CTINTERFACE> ) {
   if ( $_ =~ "CHECKEDOUT" ) {
      if ($ctdebug) {
	 print STDERR "case 1:\noutputting: '$_'\n" ;
      }
      print TMPFILE "$_" ;
   } elsif (( $_ =~ /^element \*/ ) && ( $_ =~ "/main/LATEST" ) &&
	    !( $_ =~ /\/$proj\// )) {
      if ( ! $emitted ) {
	 $emitted = 1 ;
	 print TMPFILE "element $root/... .../$branch/LATEST\n" ;
	 print TMPFILE "element $root/... $label -mkbranch $branch\n" ;
	 print TMPFILE "element $root/... /main/LATEST -mkbranch $branch\n" ;
	 if ($ctdebug) {
	    print STDERR "case 2:\n" ;
	    print STDERR "outputting: 'element $root/... .../$branch/LATEST'\n" ;
	    print STDERR "outputting: 'element $root/... $label -mkbranch $branch'\n" ;
	    print STDERR "outputting: 'element $root/... /main/LATEST -mkbranch $branch'\n" ;
	 }
      }
      if ($ctdebug) {
	 print STDERR "case 3:\n" ;
	 print STDERR "outputting: '$_'\n" ;
      }
      print TMPFILE "$_" ;
   } elsif ( $_ =~ /\/$proj\// ) {
      if ( ! $emitted ) {
	 $emitted = 1 ;
	 print TMPFILE "element $root/... .../$branch/LATEST\n" ;
	 print TMPFILE "element $root/... $label -mkbranch $branch\n" ;
	 print TMPFILE "element $root/... /main/LATEST -mkbranch $branch\n" ;
	 if ($ctdebug) {
	    print STDERR "case 4:\n" ;
	    print STDERR "outputting: 'element $root/... .../$branch/LATEST'\n" ;
	    print STDERR "outputting: 'element $root/... $label -mkbranch $branch'\n" ;
	    print STDERR "outputting: 'element $root/... /main/LATEST -mkbranch $branch'\n" ;
	 }
      }
   } else {
      if ($ctdebug) {
	 print STDERR "case 5:\n" ;
	 print STDERR "outputting: '$_'\n" ;
      }
      print TMPFILE "$_" ;
   }
}
close( CTINTERFACE ) ;
close( TMPFILE ) ;
if ($ctdebug) {
   print STDERR "output to execute: '/usr/atria/bin/cleartool setcs -tag $view $tmpname ; rm $tmpname'\n" ;
}
system "/usr/atria/bin/cleartool setcs -tag $view $tmpname ; rm $tmpname\n" ;
